//
//  RCTBEEPickerManager.m
//  RCTBEEPickerManager
//
//  Created by MFHJ-DZ-001-417 on 16/9/6.
//  Copyright © 2016年 MFHJ-DZ-001-417. All rights reserved.
//

#import "RCTBEEPickerManager.h"
#import "BzwPicker.h"
#import <React/RCTEventDispatcher.h>

@interface RCTBEEPickerManager()

@property(nonatomic,strong)BzwPicker *pick;
@property(nonatomic,assign)float height;
@property(nonatomic,weak)UIWindow * window;
@property (nonatomic, strong)UIView *translucentView;

@end

@implementation RCTBEEPickerManager

@synthesize bridge = _bridge;

- (dispatch_queue_t)methodQueue
{
  return dispatch_get_main_queue();
}

+ (BOOL)requiresMainQueueSetup
{
  return YES;
}

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(_init:(NSDictionary *)indic){
  
  dispatch_async(dispatch_get_main_queue(), ^{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
  });
  
  self.window = [UIApplication sharedApplication].keyWindow;
  
  NSString *pickerConfirmBtnText=indic[@"pickerConfirmBtnText"];
  NSString *pickerCancelBtnText=indic[@"pickerCancelBtnText"];
  NSString *pickerTitleText=indic[@"pickerTitleText"];
  NSArray *pickerConfirmBtnColor=indic[@"pickerConfirmBtnColor"];
  NSArray *pickerCancelBtnColor=indic[@"pickerCancelBtnColor"];
  NSArray *pickerTitleColor=indic[@"pickerTitleColor"];
  NSArray *pickerToolBarBg=indic[@"pickerToolBarBg"];
  NSArray *pickerBg=indic[@"pickerBg"];
  NSArray *selectArry=indic[@"selectedValue"];
  NSArray *weightArry=indic[@"wheelFlex"];
  NSString *pickerToolBarFontSize=[NSString stringWithFormat:@"%@",indic[@"pickerToolBarFontSize"]];
  NSString *pickerFontSize=[NSString stringWithFormat:@"%@",indic[@"pickerFontSize"]];
  NSString *pickerFontFamily=[NSString stringWithFormat:@"%@",indic[@"pickerFontFamily"]];
  NSArray *pickerFontColor=indic[@"pickerFontColor"];
  NSString *pickerRowHeight=indic[@"pickerRowHeight"];
  NSString *pickerSubTitleText=indic[@"pickerSubTitleText"];
  id pickerData=indic[@"pickerData"];
  
  NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
  
  dataDic[@"pickerData"]=pickerData;
  
  [self.window.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
    if ([obj isKindOfClass:[BzwPicker class]] || obj.tag == 1000) {
      dispatch_async(dispatch_get_main_queue(), ^{
        
        [obj removeFromSuperview];
      });
    }
    
  }];
  
  if ([[UIDevice currentDevice].systemVersion doubleValue] >= 9.0 ) {
    self.height=250;
  }else{
    self.height=220;
  }
  
  self.pick=[[BzwPicker alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.height) dic:dataDic leftStr:pickerCancelBtnText centerStr:pickerTitleText rightStr:pickerConfirmBtnText topbgColor:pickerToolBarBg bottombgColor:pickerBg leftbtnbgColor:pickerCancelBtnColor rightbtnbgColor:pickerConfirmBtnColor centerbtnColor:pickerTitleColor selectValueArry:selectArry weightArry:weightArry pickerToolBarFontSize:pickerToolBarFontSize pickerFontSize:pickerFontSize pickerFontColor:pickerFontColor  pickerRowHeight: pickerRowHeight pickerFontFamily:pickerFontFamily pickerSubTitleText:pickerSubTitleText];
  
  @weakify(self);
  _pick.bolock=^(NSDictionary *backinfoArry){
    @strongify(self);
    dispatch_async(dispatch_get_main_queue(), ^{
      
      [self.bridge.eventDispatcher sendAppEventWithName:@"pickerEvent" body:backinfoArry];
    });
  };
  
  dispatch_async(dispatch_get_main_queue(), ^{
    
    [self.translucentView addSubview:self->_pick];
    [self.window addSubview:self.translucentView];
  });
  
}

RCT_EXPORT_METHOD(show){
  if (self.pick) {
    
    dispatch_async(dispatch_get_main_queue(), ^{
      [UIView animateWithDuration:.3 animations:^{
        [self.translucentView setFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self->_pick setFrame:CGRectMake(0, SCREEN_HEIGHT-self.height, SCREEN_WIDTH, self.height)];
      }];
    });
  }return;
}

RCT_EXPORT_METHOD(hide){
  
  if (self.pick) {
    dispatch_async(dispatch_get_main_queue(), ^{
      [UIView animateWithDuration:.3 animations:^{
        [self->_pick setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.height)];
      } completion:^(BOOL finished) {
        [self.pick removeFromSuperview];
        [self->_translucentView removeFromSuperview];
      }];
    });
  }
  
  self.pick.hidden=YES;
  
  return;
}

RCT_EXPORT_METHOD(select: (NSArray*)data){
  
  if (self.pick) {
    dispatch_async(dispatch_get_main_queue(), ^{
      self->_pick.selectValueArry = data;
      [self->_pick selectRow];
    });
  }return;
}

RCT_EXPORT_METHOD(isPickerShow:(RCTResponseSenderBlock)getBack){
  
  if (self.pick) {
    
    CGFloat pickY=_pick.frame.origin.y;
    
    if (pickY==SCREEN_HEIGHT) {
      
      getBack(@[@YES]);
    }else
    {
      getBack(@[@NO]);
    }
  }else{
    getBack(@[@"picker不存在"]);
  }
}

#pragma mark -- getter
- (UIView *)translucentView{
  if (!_translucentView) {
    _translucentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _translucentView.tag = 1000;
    _translucentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    @weakify(self);
    UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
      @strongify(self);
      [UIView animateWithDuration:.3 animations:^{
        [self->_pick setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.height)];
        [self->_translucentView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self.bridge.eventDispatcher sendAppEventWithName:@"pickerEvent" body:@{@"type":@"cancel",@"selectedValue":@[],@"selectedIndex":@[]}];
      } completion:^(BOOL finished) {
        [self->_pick removeFromSuperview];
        [self->_translucentView removeFromSuperview];
      }];
    }];
    [_translucentView addGestureRecognizer:tapgesture];
  }
  return _translucentView;
}

@end
